package net.lgpage.http;

import java.io.IOException;
import java.net.SocketTimeoutException;

import net.lgpage.activity.BaseActivity;
import net.lgpage.http.ExceptionAsyncTask.HttpErrorCodeException;
import net.lgpage.http.ExceptionAsyncTask.HttpSessionOutTimeException;
import net.lgpage.http.ExceptionAsyncTask.NoNetworkException;
import net.lgpage.util.PromptTool.ToastUtil;

import org.apache.http.HttpException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.ConnectionPoolTimeoutException;

import com.matriq.cloudmanager.LoginActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

public class HttpExceptionToastDealer implements HttpExceptionDealer{

	public final static String[]execeptionToast = {
		"没有网络,请检测网络连接!","连接网络超时。","连接中断。","服务器处理异常。","登录信息过期"
	};
	Context context;
	
	public HttpExceptionToastDealer(Context context) {
		super();
		this.context = context;
	}

	public void deal(Exception e) throws Exception {
		if (e instanceof SocketTimeoutException) {
			ToastUtil.noretryToast(context, execeptionToast[1]);
		} else if (e instanceof ConnectTimeoutException) {
			ToastUtil.noretryToast(context, execeptionToast[1]);
		} else if (e instanceof ClientProtocolException) {
			ToastUtil.noretryToast(context, execeptionToast[1]);
		} else if (e instanceof ConnectionPoolTimeoutException) {
			ToastUtil.noretryToast(context, execeptionToast[1]);
		} else if (e instanceof IOException) {
			ToastUtil.noretryToast(context, execeptionToast[2]);
		} else if (e instanceof HttpException) {
			ToastUtil.noretryToast(context, execeptionToast[3]);
		} else if (e instanceof HttpErrorCodeException){
			ToastUtil.noretryToast(context, e.getMessage() + "");
		}else if(e instanceof HttpSessionOutTimeException){
			ToastUtil.noretryToast(context, execeptionToast[4]);
			Activity ac = (Activity) context;
			ac.setResult(BaseActivity.SESSION_OUT);
			ac.finish();
			Intent it = new Intent();
			it.setClass(context, LoginActivity.class);
			ac.startActivity(it);
			throw e;
		}else if(e instanceof NoNetworkException){
			ToastUtil.noretryToast(context, execeptionToast[0]);
		}
	}

}
